package com.xu.sms.controller;


import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.xu.sms.R;
import com.xu.sms.adapter.SHistoryAdapter;
import com.xu.sms.db.MySqliteOpenHelper;
import com.xu.sms.entity.SHistory;
import com.xu.sms.service.SHistoryService;
import com.xu.sms.utils.LogUtil;

public class SearchActivity extends Activity {
	
	private Context context;
	private ListView search_ll_search;
	private MySqliteOpenHelper msoh;
	private SHistoryService sHistoryService;
	private String TAG = SearchActivity.class.getSimpleName();
	private List<SHistory> shList ;
	private MyOnItemClickListener myOnItemClickListener;
	public static int  delActionCode = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		context = this;
		search_ll_search = (ListView) findViewById(R.id.search_ll_search);
		/*
		 * 1.将搜索的关键字保存到数据库中（当你单击搜索的时我就保存）
		 * 2.关键字列表的展示
		 */
	    msoh = new MySqliteOpenHelper(context);
		 //并在用户的私有的目录创建一个数据库 
		msoh.getReadableDatabase();
		sHistoryService = new SHistoryService(msoh);
		setItemData();
	}



	//查询数据看并设置条目数据
	private void setItemData() {
		shList = sHistoryService.findAll();
		if(shList.size() >=0){
			myOnItemClickListener = new MyOnItemClickListener();
			search_ll_search.setAdapter(new SHistoryAdapter(shList, context,handler));
			search_ll_search.setOnItemClickListener(myOnItemClickListener);
		}
	}
	
	
	
	//给搜索框设置单击事件
	public void search(View view){
		EditText search_et_input = (EditText) findViewById(R.id.search_et_input);
		if(TextUtils.isEmpty(search_et_input.toString())){
			Toast.makeText(context, "搜索的内容不能为空", Toast.LENGTH_SHORT).show();
			return ;
		}
		String keyword = search_et_input.getText().toString().trim();
		//保存关键字到数据库中
		sHistoryService.insert(keyword);
		Intent intent = new Intent(context,FBOInfoActivity.class);
		intent.putExtra("keyword", keyword);
		startActivity(intent);
	}
	
	@Override
	protected void onResume() {
        super.onResume();
        setItemData();
        // The activity has become visible (it is now "resumed").
    }
	
	public void clearAllHistory(View view){
		sHistoryService.delAll();
		setItemData();
		Toast.makeText(context, "恭喜老大，成功清空全部搜索历史记录", Toast.LENGTH_SHORT).show();
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what == delActionCode){
				Integer id = (Integer)msg.obj;
				sHistoryService.delete(id);
				//不起作用
				setItemData();
			}
		}
	};
	
	class MyOnItemClickListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			//单击条目实现按关键字搜索信息
			SHistory sh = shList.get(position);
			String keyword = sh.getKeyword();
			Intent intent = new Intent(context,FBOInfoActivity.class);
LogUtil.i(TAG, 105, keyword);
			intent.putExtra("keyword", keyword);
			startActivity(intent);
		}
	}
	
	
}
